#include "appcontroller.h"
#include <QCoreApplication>


AppController::AppController(QObject *parent)
    : QObject(parent)
{
    _mqtt = new MqttClient(this);      // 父对象为 this，Qt 自动管理内存
    _serial = new SerialManager(this); // 父对象为 this

    connect(_serial, &SerialManager::closeSerialmanager,
                      this, &AppController::stopLogging);


    // 信号槽连接，不用 lambda
    bool ok = connect(_mqtt, &MqttClient::forwardPrepared,
            this, &AppController::handleForwardPrepared);

    // 连接 MQTT 接收 -> 串口发送
    // connect(_mqtt, &MqttClient::messageReceived,
    //         this, [=](const QString &topic, const QByteArray &msg) {
    //     if (_mqttForward && _serial->isOpen()) {
    //         _serial->writeData(msg);
    //     }
    // });
    // bool ok = connect(_mqtt, &MqttClient::forwardPrepared, this, [=](const QByteArray &forwardMsg) {
    //     qDebug() << "forwardMsg:" << forwardMsg;
    //     if (_mqttForward && _serial->isOpen()) {
    //         qDebug() << "forwardMsg:2" ;
    //         _serial->writeData(forwardMsg);
    //     }
    // });
    qDebug() << "connect ok?" << ok;
}

void AppController::setMqttForward(bool enable)
{
    if (_mqttForward == enable) return;
    _mqttForward = enable;
    emit mqttForwardChanged();
}

void AppController::handleForwardPrepared(const QByteArray &forwardMsg)
{
    // qDebug() << "handleForwardPrepared got:" << forwardMsg;
    if (_mqttForward && _serial->isOpen()) {
        // qDebug() << "handleForwardPrepared writing serial...";

        if (!_isRecording)
            startLogging();   // 首次写入时开始日志

        _serial->writeData(forwardMsg);

        // 当前时间戳（精确到毫秒）
        qint64 msSinceStart = QDateTime::currentMSecsSinceEpoch() % 86400000; // 当天毫秒数（00:00起）
        QString msStr = QString::number(msSinceStart);


        // 解析 forwardMsg，例如 "$TGT,lonInt,latInt,altInt"
        QString msgStr = QString::fromUtf8(forwardMsg).trimmed();
        QStringList parts = msgStr.split(QLatin1Char(','));

        if (parts.size() >= 4) {
            QString lonInt = parts[1];
            QString latInt = parts[2];
            QString altInt = parts[3];
            QString logLine = QString(tr("%1,%2,%3,%4"))
                                  .arg(msStr)
                                  .arg(lonInt)
                                  .arg(latInt)
                                  .arg(altInt);
            writeLogLine(logLine);
        }
    }
}

// 启动日志记录
void AppController::startLogging()
{
    if (_isRecording)
        return;

    QString timestamp = QDateTime::currentDateTime().toString(tr("yyyyMMdd_hhmmss_zzz"));
    QString basePath  = QCoreApplication::applicationDirPath() + tr("/log");

    // ✅ 2若目录不存在则自动创建
    QDir dir(basePath);
    if (!dir.exists()) {
        dir.mkpath(basePath);
    }

    // csv 日志
    QString csvFileName = QString(tr("%1/%2.csv")).arg(basePath).arg(timestamp);

    _csvFile = new QFile(csvFileName);

    bool csvOk = _csvFile->open(QIODevice::WriteOnly | QIODevice::Text);

    if ( csvOk)
    {
        _csvStream.setDevice(_csvFile);
        _isRecording = true;
        // ✅ 写入 CSV 表头
        _csvStream << "Time,Longitude,Latitude,Altitude(cm)\n";
        // ✅ 2️⃣ 写入文件创建时间（第二行）
        QString dateTimeStr = QDateTime::currentDateTime().toString(tr("yyyy-MM-dd hh:mm:ss.zzz"));
        _csvStream << dateTimeStr << "\n";

        qDebug() << "开始日志录制:" << csvFileName;
    }
    else
    {
        qWarning() << "无法创建日志文件";
        if (_csvFile) { delete _csvFile; _csvFile = nullptr; }
    }
}

// 停止日志记录
void AppController::stopLogging()
{
    if (_isRecording)
    {
        if (_csvFile) {
            _csvStream.flush();
            _csvFile->close();
            delete _csvFile;
            _csvFile = nullptr;
        }

        _isRecording = false;
        qDebug() << "日志录制结束";
    }
}

// 写入一行数据
void AppController::writeLogLine(const QString &line)
{
    if (!_isRecording)
        return;


    if (_csvFile) {
        _csvStream << line << "\n";
        _csvStream.flush();
    }
}

